﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using ERP.Manager.BusinessServices;
using ERP.Manager.DataEntity;
using ERP.Manager.Controllers.CustomAttributes;
namespace ERP.Manager.Controllers
{
    [CheckLoginAttribute]
    public class UserinfoController : Controller
    {
        private readonly UserinfoService userService;
        private readonly DepartmentService departmentservice;
        private readonly MessageinfoService messageservice;
        private readonly WorkinfoService workservice;
        private readonly ContextService contextservice;
        public UserinfoController() { }
        public UserinfoController(UserinfoService userservice, LoginService loginservice, DepartmentService departmentservice, MessageinfoService messageservices, WorkinfoService workservices, ContextService contextservices)
        {
            this.userService = userservice;
            this.departmentservice = departmentservice;
            this.messageservice = messageservices;
            this.workservice = workservices;
            this.contextservice = contextservices;
        }

        #region 显示用户页面
        [CheckLoginAttribute]
        public ActionResult ShowUser(int? page)
        {
            List<UserInfo> list = null;
            int pagecount = page ?? 1;
            list = userService.getAllUsersPagecount(pagecount);
            int listCount = userService.getAllUsersCount();
            ViewData["userinfo"] = list;
            ViewData["pageCount"] = listCount;
            return View();
        }
        #endregion

        #region 添加用户页面
        [CheckPersonnel]
        public ActionResult AddUser()
        {
            ViewData["department"] = departmentservice.getDepartments();
            return View();
        }
        #endregion

        #region 修改用户信息页面
        [CheckPersonnel]
        public ActionResult UpdateUser(string userid)
        {
            ViewData["userinfo"] = userService.DetailUserinfo(userid);
            ViewData["department"] = departmentservice.getDepartments();
            return View();
        }
        #endregion

        #region 修改个人用户信息页面
        [CheckLoginAttribute]
        public ActionResult UserInfoUpdate(string userId)
        {
            ViewData["userinfo"] = userService.DetailUserinfo(userId);
            return View();
        }
        #endregion

        #region ajax添加用户信息
        [AcceptVerbs(HttpVerbs.Post)]
        public string AjaxAddUser(string userid, string username, string userpwd, string userage, string userphonenumber, string usersex, string userdepartname, string useraddress)
        {
            try
            {
                UserInfo userinfo = new UserInfo();
                userinfo.userId = userid.Trim();
                userinfo.userName = username.Trim();
                userinfo.sex = usersex.Trim();
                userinfo.address = useraddress.Trim();
                userinfo.age = int.Parse(userage.Trim());
                userinfo.startingTime = DateTime.Now.AddDays(0);
                userinfo.password = userpwd.Trim();
                userinfo.DepartmentInfo.departmentId = userdepartname.Trim();
                userinfo.phoneNumber = userphonenumber.Trim();
                int rowcount = userService.addUser(userinfo);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return "" + ex.Message;
            }
        }
        #endregion

        #region Ajax根据编号删除用户信息
        [CheckPersonnel]
        public string DeleteUser(string userid)
        {
            try
            {
                List<Messageinfo> listmessage = messageservice.getallmessagefromuserid(userid);
                List<WorkInfo> listworkinfo = workservice.getallworkfromuserid(userid);

                if (contextservice.UserId == userid)
                {
                    return "error";
                }
                else if (listmessage.Count > 0 || listworkinfo.Count > 0)
                {
                    return "error";
                }
                else
                {
                    UserInfo userinfo = new UserInfo();
                    userinfo.userId = userid.ToString();

                    int rowcount = userService.DeleteUserinfo(userinfo);
                    if (rowcount > 0)
                    {
                        return "success";
                    }
                    else
                    {
                        return "error";
                    }
                }
            }
            catch (Exception ex)
            {
                return "" + ex.Message;
            }
        }
        #endregion

        #region ajax修改用户信息
        [AcceptVerbs(HttpVerbs.Post)]
        public string AjaxUpdateUserinfo(string userid, string age, string phonenumber, string sex, string departmentID, string address)
        {
            try
            {
                UserInfo user = userService.DetailUserinfo(userid);
                user.userId = userid;
                user.age = int.Parse(age);
                user.phoneNumber = phonenumber;
                user.sex = sex;
                user.password = user.password;
                user.DepartmentInfo.departmentId = departmentID;
                user.address = address;
                int rowcount = userService.UpdateUserinfo(user);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "修改用户信息成功！！";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "修改用户信息失败！！";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return "" + ex.Message;
            }
        }
        #endregion

        #region 修改个人信息
        [AcceptVerbs(HttpVerbs.Post)]
        public string AjaxUserinfoUpdate(string userid, string userage, string pwd, string phonenuber, string usersex, string useraddress)
        {
            try
            {
                UserInfo user = userService.DetailUserinfo(userid);
                user.userId = userid;
                user.sex = usersex;
                user.address = useraddress;
                user.age = int.Parse(userage.ToString());
                user.password = pwd;
                user.DepartmentInfo.departmentId = user.DepartmentInfo.departmentId;
                user.phoneNumber = phonenuber;
                int rowcount = userService.UpdateUserinfo(user);
                if (rowcount > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    return "success";
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    return "error";
                }
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(1000);
                return "" + ex.Message;
            }
        }
        #endregion

        #region 用ajax验证用户编号是否存在
        [AcceptVerbs(HttpVerbs.Post)]
        public string AjaxuserIDValidator(string userid)
        {
            UserInfo user = userService.DetailUserinfo(userid);
            if (user != null && user.userId == userid)
            {
                System.Threading.Thread.Sleep(1000);
                return "error";
            }
            else
            {
                System.Threading.Thread.Sleep(1000);
                return "success";
            }
        }
        #endregion

        #region 用ajax验证用户姓名是否存在的方法
        [AcceptVerbs(HttpVerbs.Post)]
        public string AjaxuserNameValidator(string userName)
        {
            UserInfo user = userService.DetailUserinfousername(userName);
            if (user != null && user.userName == userName)
            {
                System.Threading.Thread.Sleep(1000);
                return "error";
            }
            else
            {
                System.Threading.Thread.Sleep(1000);
                return "success";
            }
        }
        #endregion

        #region 用AJax验证用户密码是否正确
        [AcceptVerbs(HttpVerbs.Post)]
        public string AjaxuserPwdValidator(string userid, string userpwd)
        {
            UserInfo user = userService.Detailuserpassword(userid, userpwd);
            if (user != null && user.password == userpwd)
            {
                System.Threading.Thread.Sleep(1000);
                return "success";
            }
            else
            {
                System.Threading.Thread.Sleep(1000);
                return "error";
            }
        }
        #endregion
    }
}
